namespace HZY.Framework.Core.CustomExceptionHandling;

/// <summary>
/// 自定义异常扩展
/// </summary>
public static class CustomExceptionHandlingExtensions
{
    /// <summary>
    /// 添加自定义异常拦截
    /// </summary>
    /// <param name="builder"></param>
    public static void AddCustomExceptionHandling(this WebApplicationBuilder builder)
    {
        builder.Services.AddProblemDetails(opt =>
        {
            opt.CustomizeProblemDetails = context =>
            {
                context.ProblemDetails.Extensions.TryAdd("status", context.HttpContext.Response.StatusCode);
                context.ProblemDetails.Extensions.TryAdd("code", context.HttpContext.Response.StatusCode);
                context.ProblemDetails.Extensions.TryAdd("timestamp", DateTimeOffset.Now.ToUnixTimeMilliseconds());

                context.HttpContext.Items.TryGetValue(nameof(ProblemDetailsContext), out var result);

                if (result is ProblemDetailsContext problemDetailsContext &&
                    problemDetailsContext.Exception is MessageBoxException messageBoxException)
                {
                    var r = messageBoxException.GetResult();
                    context.ProblemDetails = problemDetailsContext.ProblemDetails;

                    context.ProblemDetails.Extensions.TryAdd("message", context.ProblemDetails.Detail);
                    context.ProblemDetails.Extensions.TryAdd("errorMessage", r.ErrorMessage);
                    context.ProblemDetails.Extensions.TryAdd("data", r.Data);
                }

                //
                // context.ProblemDetails = new ProblemDetails()
                // {
                //     Title = context.ProblemDetails.Detail,
                //     Detail = context.ProblemDetails.Detail,
                // };
                // context.ProblemDetails.Extensions.TryAdd("status", context.HttpContext.Response.StatusCode);
                // context.ProblemDetails.Extensions.TryAdd("code", context.HttpContext.Response.StatusCode);
                // context.ProblemDetails.Extensions.TryAdd("message", context.ProblemDetails.Detail);
                // context.ProblemDetails.Extensions.TryAdd("errorMessage", context.ProblemDetails.Detail);
                // context.ProblemDetails.Extensions.TryGetValue("data", out var data);
                // context.ProblemDetails.Extensions.TryAdd("timestamp", DateTimeOffset.Now.ToUnixTimeMilliseconds());

                // context.ProblemDetails.Instance =
                //     $"{context.HttpContext.Request.Protocol} {context.HttpContext.Request.Method} {context.HttpContext.Request.Path}";
                // context.ProblemDetails.Extensions.TryAdd("requestId", context.HttpContext.TraceIdentifier);
                //
                // var activity = context.HttpContext.Features.Get<IHttpActivityFeature>()?.Activity;
                // context.ProblemDetails.Extensions.TryAdd("traceId", activity?.Id);
                //
                // if (context.HttpContext.Connection.RemoteIpAddress != null)
                // {
                //     context.ProblemDetails.Extensions.TryAdd("clientIP",
                //         context.HttpContext.Connection.RemoteIpAddress?.ToString());
                // }
            };
        });

        // 添加异常
        // builder.Services.AddExceptionHandler<ProblemExceptionHandler>();

        builder.Services.AddScoped<CustomExceptionHandlingMiddleware>();
    }

    /// <summary>
    /// 使用自定义异常拦截
    /// </summary>
    /// <param name="builder"></param>
    public static void UseCustomExceptionHandling(this WebApplication builder)
    {
        // builder.UseExceptionHandler();

        builder.UseMiddleware<CustomExceptionHandlingMiddleware>();
    }
}